﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Smrf.NodeXL.Core;
using Smrf.NodeXL.Layouts;
//using Microsoft.NodeXL.Visualization.Wpf;
using Smrf.NodeXL.Adapters;
using pptGraphCreatorAddIn.Skins;


namespace pptGraphCreatorAddIn
{
    public static class OfficeHelper
    {
        public static PowerPoint.Shape AddNode(float size,float PosX,float PosY,PowerPoint.Shapes shapes)
        {
            shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeOval,PosX, PosY, size, size);
            PowerPoint.Shape sp = shapes[shapes.Count];//It starts from 1 not from 0                    
            
            return sp;
        }
        

        public static IGraph CreateGraph(int VerticesCount, float Width,float Hight, string Layout="")
        {
            if (VerticesCount > 0)
            {
                IGraph G = new Graph(GraphDirectedness.Undirected);
                IVertexCollection vertices = G.Vertices;
                IVertex[] VerticiesCol = new IVertex[VerticesCount];

                //Store the instance of the vertices.
                for (int i = 0; i < VerticesCount; i++)
                {
                    VerticiesCol[i] = vertices.Add();
                }

                //We have can have a maximum of n(n-1)/2 edge
                //We randomly set the number of edges.
                Random r = new Random();
                int EdgesCount =r.Next(VerticesCount, VerticesCount * (VerticesCount - 1)/2+1);

                IEdgeCollection Edges  = G.Edges;

                while(Edges.Count< EdgesCount)
                {
                    //Randomly select two vertices.
                    IVertex V1= VerticiesCol[r.Next(0, VerticesCount)];                    
                    IVertex V2= VerticiesCol[r.Next(0, VerticesCount)];
                                        
                    var x = from e in Edges
                            where (e.Vertex1 == V1 && e.Vertex2 == V2) || (e.Vertex1 == V2 && e.Vertex2 == V1)
                            select e;
                    if (x.Count() == 0)
                        //And Add edge between them
                        Edges.Add(V1, V2);
                }


                if (!String.IsNullOrEmpty(Layout))
                {
                    System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, (int)Width, (int)Hight);
                    GraphLayoutFactory.ApplyLayout(G, Layout, rect);                
                }
                
                
                return G;
            }
            else
                return null;
        }

        public static IGraph LoadGraph(string FilePath)
        {
            if (!System.IO.File.Exists(FilePath))
            {
                return null;
            }

            GraphMLGraphAdapter GmlAdapter = new GraphMLGraphAdapter();

            return GmlAdapter.LoadGraphFromFile(FilePath);
        }


        public static void ApplySkin(ISkin skin)
        {
            var slide = (PowerPoint.Slide)Globals.ThisAddIn.Application.ActiveWindow.View.Slide;

            foreach (PowerPoint.Shape item in slide.Shapes)
            {
                if (item.AutoShapeType == Microsoft.Office.Core.MsoAutoShapeType.msoShapeOval && item.Name.Contains("Node_"))
                {
                    skin.Apply(item);
                }
            }
        }
    }
}
